package com.stylefeng.guns.modular.netcar.service.impl;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.stylefeng.guns.core.model.CommonKeyValue;
import com.stylefeng.guns.core.model.Pager;
import com.stylefeng.guns.modular.netcar.dao.VehiclePriceTypeMapper;
import com.stylefeng.guns.modular.netcar.model.LinePrice;
import com.stylefeng.guns.modular.netcar.model.OrderInf;
import com.stylefeng.guns.modular.netcar.model.VehiclePriceType;
import com.stylefeng.guns.modular.netcar.service.ILinePriceService;
import com.stylefeng.guns.modular.netcar.service.IOrderInfService;
import com.stylefeng.guns.modular.netcar.service.IVehiclePriceTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.toolkit.IdWorker;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**

 * 车型价格 Service 实现 
 * @author sasazaixian
 * @Date 2018-11-05 17:13
 */
@Service
public class VehiclePriceTypeServiceImpl extends ServiceImpl<VehiclePriceTypeMapper,VehiclePriceType> implements IVehiclePriceTypeService {

    @Resource
    private VehiclePriceTypeMapper vehiclePriceTypeMapper;

    @Autowired
    private ILinePriceService linePriceService;

    @Autowired
    private IOrderInfService orderInfService;

    /**
     * 删除车型价格
     * @param vehiclePriceType
     * @return     
     */
    @Transactional
    public int deleteVehiclePriceType(VehiclePriceType vehiclePriceType,String token)throws Exception{
        if(vehiclePriceType==null ){
            throw new RuntimeException("参数不全！");
        }
        List<VehiclePriceType> vehiclePriceTypeList=this.vehiclePriceTypeMapper.selectListVehiclePriceType(vehiclePriceType);
        for(VehiclePriceType en:vehiclePriceTypeList){
            String vptcode=en.getVptCode();

            LinePrice linePrice=new LinePrice();
            linePrice.setVptCode(vptcode);
            List list=linePriceService.selectListLinePrice(linePrice,token);
            if(list!=null && list.size()>0){
                throw new RuntimeException("已经在线路价格中引用，不能删除！");
            }
            OrderInf orderInf=new OrderInf();
            orderInf.setVptCode(vptcode);
            list=orderInfService.selectListOrderInf(orderInf,token);
            if(list!=null && list.size()>0){
                throw new RuntimeException("已经在线路价格中引用，不能删除！");
            }
        }
        return this.vehiclePriceTypeMapper.deleteVehiclePriceType(vehiclePriceType);
    }
    /**
     * 批量删除车型价格
     * @param ids 主键ID数组
     * @return 
     */
    @Transactional
    public int deleteBatchVehiclePriceType(String[] ids,String token)throws Exception{
        for(String id:ids){
            String vptcode=this.findVehiclePriceTypeById(id,token).getVptCode();
            LinePrice linePrice=new LinePrice();
            linePrice.setVptCode(vptcode);
            List list=linePriceService.selectListLinePrice(linePrice,token);
            if(list!=null && list.size()>0){
                throw new RuntimeException("已经在线路价格中引用，不能删除！");
            }
            OrderInf orderInf=new OrderInf();
            orderInf.setVptCode(vptcode);
            list=orderInfService.selectListOrderInf(orderInf,token);
            if(list!=null && list.size()>0){
                throw new RuntimeException("已经在线路价格中引用，不能删除！");
            }
        }
        return this.vehiclePriceTypeMapper.deleteBatchVehiclePriceType(ids);
    }
    /**
     * 查询车型价格列表
     * @param vehiclePriceType 车型价格对象
     * @return
     */
    public List<VehiclePriceType> selectListVehiclePriceType(VehiclePriceType vehiclePriceType,String token)throws Exception{
        return  this.vehiclePriceTypeMapper.selectListVehiclePriceType(vehiclePriceType);
    }
    /**
	 * 分页查询车型价格列表
     * @param pager 分页对象
     * @return
     */     
    public  Map pageListVehiclePriceType(Pager pager,String token) throws Exception{
        Page page=new Page(pager.getPageNumber(),pager.getPageSize());
        page.setRecords(vehiclePriceTypeMapper.selectListVehiclePriceType(page,(VehiclePriceType)pager.getObj()));
        List<VehiclePriceType> list = page.getRecords();
        Map mapDate = new HashMap();
        mapDate.put("rows", list);
        mapDate.put("total", page.getTotal());
        mapDate.put("page", page.getCurrent());
        mapDate.put("pageCount", page.getPages());
        return mapDate;
    }
    /**
     * 同一个事务 互斥锁查询
     * @param vehiclePriceType 车型价格对象
     * @return 
     */
    public  List<VehiclePriceType> selectListVehiclePriceTypeForUpdate(VehiclePriceType vehiclePriceType)throws Exception{
        return  this.vehiclePriceTypeMapper.selectListVehiclePriceTypeForUpdate(vehiclePriceType);
    }
    /**
     * 通过主键id查询的车型价格列表
     * @param id 主键id
     * @return     
     */
    public  VehiclePriceType findVehiclePriceTypeById(String id,String token)throws Exception{
        return  this.vehiclePriceTypeMapper.findVehiclePriceTypeById(id);
    }

    /**
     * 新增车型价格数据
     * @param vehiclePriceType  车型价格对象
     * @return
     */
    @Transactional
    public int insertVehiclePriceType(VehiclePriceType vehiclePriceType,String token) throws Exception { 
        if(vehiclePriceType!=null && (vehiclePriceType.getVptId()==null || vehiclePriceType.getVptId().trim().length()<10)){
            vehiclePriceType.setVptId(IdWorker.get32UUID());
        }  
        return this.vehiclePriceTypeMapper.insertVehiclePriceType(vehiclePriceType);
    }
    
    /**
     * 更新车型价格数据
     * @param vehiclePriceType 车型价格对象
     * @return     
     */
    @Transactional
    public int updateVehiclePriceType(VehiclePriceType vehiclePriceType,String token) throws Exception {     	
        return  this.vehiclePriceTypeMapper.updateVehiclePriceType(vehiclePriceType);
    }    
    /**
     * 查询车型价格返回key-Value属性
     * @param vehiclePriceType 系统操作对象
     * @return     
     */
    public List<CommonKeyValue> optionsVehiclePriceType(VehiclePriceType vehiclePriceType,String token) throws Exception {
        List<VehiclePriceType> listVehiclePriceType=this.vehiclePriceTypeMapper.selectListVehiclePriceType(vehiclePriceType);
        List<CommonKeyValue> listOptions=new ArrayList<CommonKeyValue>();
        for(VehiclePriceType item:listVehiclePriceType){
            CommonKeyValue opts=new CommonKeyValue();
            opts.setId(""+item.getVptId());
            opts.setValue(item.getVptId());
            listOptions.add(opts);
        }
        return listOptions;
    }
}
